package com.wn.service.doctorService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.MedicalRecordDetailMapper;
import com.wn.mapper.MedicalRecordMapper;
import com.wn.ov.DruglistOv;
import com.wn.ov.MedicalRecordOv;
import com.wn.ov.PatientOv;
import com.wn.pojo.MedicalRecord;
import com.wn.pojo.MedicalRecordDetail;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class MedicalRecordService {
    @Autowired
    private MedicalRecordMapper medicalRecordMapper;

    @Autowired
    private MedicalRecordDetailMapper medicalRecordDetailMapper;

    public ServerResponse addMedicalRecord( MedicalRecord medicalRecord) {
        MedicalRecord mr = medicalRecordMapper.selectByPIdAndDId(medicalRecord.getPatientId());
        if (StringUtils.isEmpty(mr)){
            medicalRecordMapper.insert(medicalRecord);
            return  new ServerResponse(ResponseCode.SUCCESS,"增加成功");
        }
        else {
            medicalRecordMapper.updateByPrimaryKeySelective(medicalRecord);
            return  new ServerResponse(ResponseCode.ERROR,"病历已经存在，已修改");
        }
    }

    public ServerResponse getMedicalRecord(int patientId) {
        MedicalRecord medicalRecord=medicalRecordMapper.selectByPIdAndDId(patientId);
        if (!StringUtils.isEmpty(medicalRecord)){
            return  new ServerResponse(ResponseCode.SUCCESS,medicalRecord,"成功");
        }
        return  new ServerResponse(ResponseCode.ERROR,medicalRecord,"失败");
    }


    //查询病人的病例
    public ServerResponse getMedicalRecordOv(PatientOv patientOv) {
        MedicalRecordOv medicalRecordOv = medicalRecordMapper.getMedicalRecordOv(patientOv.getId());
//        if(StringUtils.isEmpty(medicalRecordOv)){
//            return  new ServerResponse(ResponseCode.ERROR,"失败");
//        }else{
            return  new ServerResponse(ResponseCode.SUCCESS,medicalRecordOv,"成功");
//        }
    }

    //增加病人的病例详情
    public ServerResponse insertMedicalRecordDetail(MedicalRecordDetail medicalRecordDetail) {
        int i = medicalRecordDetailMapper.insertSelective(medicalRecordDetail);
        if(i ==0){
            return  new ServerResponse(ResponseCode.ERROR,"失败");
        }else{
            return  new ServerResponse(ResponseCode.SUCCESS,"成功");
        }
    }


    //修改病人的病例详情
    public ServerResponse updateMedicalRecordDetail(MedicalRecordDetail medicalRecordDetail,Integer id) {
        medicalRecordDetail.setUpdateUserId(id);
        int i = medicalRecordDetailMapper.updateByPrimaryKeySelective(medicalRecordDetail);
        if(i ==0){
            return  new ServerResponse(ResponseCode.ERROR,"失败");
        }else{
            return  new ServerResponse(ResponseCode.SUCCESS,"成功");
        }
    }

    public ServerResponse updateMedicalRecord(MedicalRecord medicalRecord) {
        int result = medicalRecordMapper.updateByPrimaryKeySelective(medicalRecord);
        if (result>0){
            return  new ServerResponse(ResponseCode.SUCCESS,"成功");
        }
        return  new ServerResponse(ResponseCode.ERROR,"失败");
    }

    public ServerResponse deleteMedicalRecordDetail(MedicalRecordDetail medicalRecordDetail) {
        int result = medicalRecordDetailMapper.deleteByPrimaryKey(medicalRecordDetail.getId());
        if (result>0){
            return  new ServerResponse(ResponseCode.SUCCESS,"成功");
        }
        return  new ServerResponse(ResponseCode.ERROR,"失败");
    }
}
